/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Data; 
using EXtremecode.Utils;
using EXtremecode.DataAccessLayer;
using EXtremecode.Common;
namespace EXtremecode.BusinessLayer
{
	/// <summary>
	/// Summary description for BODataSetParent.
	/// </summary>
	public class BODataSetParent:DataSet,ICachedDataSet
	{
		public BODataSetParent(string dataSetName):base(dataSetName){}
		public BODataSetParent():base("BODataSet"){}
		#region ICachedDataSet Members

		public DataTable this[string tableName]
		{
			get
			{
				DataTable dt=base.Tables[tableName];
				RefreshDataTable(dt);
				return dt;
			}
		
		}
		private void  RefreshDataTable(DataTable dt)
		{
			
			//first refresh its parents
			foreach(DataRelation relParent in dt.ParentRelations)
			{
				RefreshDataTable(relParent.ParentTable); 
			}


			if(dt.ExtendedProperties.ContainsKey(Constants.NEED_TO_REFRESH))
			{
				bool needToRefresh=(bool)dt.ExtendedProperties[Constants.NEED_TO_REFRESH];
					
				if(needToRefresh)
				{
					//fill datatable again.
					string connectionId=dt.ExtendedProperties[Constants.CONNECTION_ID].ToString();
					string viewName=dt.ExtendedProperties[Constants.VIEW_NAME].ToString();
					DataAccessor.FillDataTable(viewName,dt,connectionId,false);
					dt.ExtendedProperties[Constants.NEED_TO_REFRESH]=false;
				}
					
			}
			
		}
		


		public void Refresh(string tableName)
		{
			DataTable dt=base.Tables[tableName];
			dt.ExtendedProperties[Constants.NEED_TO_REFRESH]=true;
		}

		#endregion
	}
}
